﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SaveTheChildren_SaveTheChildren : System.Web.UI.Page
{
    DataAccess App;
    int Sender;

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {

                if (Request.QueryString.HasKeys())
                {
                    if (Request.QueryString["listid"] != String.Empty)
                        Session["listid"] = Request.QueryString["listid"];
                    else
                        Session["listid"] = 1;


                    if (Request.QueryString["dnis"] != null)
                    {
                        Session["dnis"] = Request.QueryString["dnis"];
                    }
                    else
                    {
                        Session["dnis"] = "9999999999";
                    }

                    Session["servername"] = Request.QueryString["servername"];
                    Session["projectname"] = Request.QueryString["projectname"];
                    Session["campaignname"] = Request.QueryString["campaignname"];
                    Session["agentid"] = Request.QueryString["agentid"];
                    Session["agentfirst"] = Request.QueryString["agentfirst"];
                    Session["agentlast"] = Request.QueryString["agentlast"];

                }

                App = new DataAccess();
                Session["App"] = App;
                WebWizard.ActiveStepIndex = 2;
                App.PutCallData("ScriptStart", System.DateTime.Now.ToString());
                App.PutCallData("AgentFirst", Session["agentfirst"].ToString());
                App.PutCallData("AgentLast", Session["agentlast"].ToString());
                App.PutCallData("disposition", "SR");

                App.CommitCallData();
                Sender = 2;

            }
            else
            {
                App = (DataAccess)Session["App"];

            }
        }
        catch (Exception ex)
        {
            lblException.Visible = true;
            lblException.Text = "CONTACT IT/SUPERVISOR with the following information or screenshot:" + ex.ToString();
        }

    }

    //POST LOGIC - For Step or Start pages
    protected void WebWizard_NextButtonClick(object sender, WizardNavigationEventArgs e)
    {
        try
        {
            int page = WebWizard.ActiveStepIndex;


            if (page == WebWizard.WizardSteps.IndexOf(this.Introduction))
            {

                WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.ChildSponsor);
                return;
            }

            if (page == WebWizard.WizardSteps.IndexOf(this.MoreInfo))
            {
                WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.Disposition);
                return;
            }

            if (page == WebWizard.WizardSteps.IndexOf(this.ChildSponsor))
            {
                WebWizard.ActiveStepIndex =  WebWizard.WizardSteps.IndexOf(this.ProgrSponsor);
                return;
            }
            if (page == WebWizard.WizardSteps.IndexOf(this.ProgrSponsor))
            {
                return;
            }
            if (page == WebWizard.WizardSteps.IndexOf(this.OneTime))
            {

                return;
            }
            if (page == WebWizard.WizardSteps.IndexOf(this.Rebuttals))
            {
                WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.Rebuttals);
                return;
            }
            if (page == WebWizard.WizardSteps.IndexOf(this.FAQ))
            {
                WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.FAQ);
                return;
            }
            if (page == WebWizard.WizardSteps.IndexOf(this.Disposition))
            {

                return;
            }
        }
        catch (Exception ex)
        {
            lblException.Visible = true;
            lblException.Text = ex.ToString();
        }


    }

    //POST LOGIC for finish pages
    protected void WebWizard_FinishButtonClick(object sender, WizardNavigationEventArgs e)
    {
        try
        {
            int page = WebWizard.ActiveStepIndex;


            if (page == WebWizard.WizardSteps.IndexOf(this.Introduction))
            {
                return;
            }

            if (page == WebWizard.WizardSteps.IndexOf(this.MoreInfo))
            {
                Sender = WebWizard.WizardSteps.IndexOf(this.MoreInfo);
                WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.Disposition);
                return;
            }

            if (page == WebWizard.WizardSteps.IndexOf(this.ChildSponsor))
            {
                Sender = WebWizard.WizardSteps.IndexOf(this.ChildSponsor);
                WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.Disposition);
                return;
            }
            if (page == WebWizard.WizardSteps.IndexOf(this.ProgrSponsor))
            {
                Sender = WebWizard.WizardSteps.IndexOf(this.ProgrSponsor);
                WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.Disposition);
                return;
            }
            if (page == WebWizard.WizardSteps.IndexOf(this.OneTime))
            {
                Sender = WebWizard.WizardSteps.IndexOf(this.OneTime);
                WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.Disposition);
                return;
            }
            if (page == WebWizard.WizardSteps.IndexOf(this.Rebuttals))
            {

                WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.Disposition);
                return;
            }
            if (page == WebWizard.WizardSteps.IndexOf(this.FAQ))
            {

                WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.Disposition);
                return;
            }
            if (page == WebWizard.WizardSteps.IndexOf(this.Disposition))
            {

                return;
            }
        }
        catch (Exception ex)
        {
            lblException.Visible = true;
            lblException.Text = ex.ToString();
        }


    }

    //PRE-LOGIC
    protected void WebWizard_ActiveStepChanged(object sender, EventArgs e)
    {

        if (WebWizard.ActiveStep.ID.ToString() == "Introduction")
        {

        }
        if (WebWizard.ActiveStep.ID.ToString() == "MoreInfo")
        {
            ddlProgramSponsorship.SelectedIndex = 0;
            txtOneTimeDonation.Text = "";
            txtChildrenToSponsor.Text = "";
            ddlPaymentType.SelectedIndex = 1;
            ddlPaymentType_SelectedIndexChanged(null, null);
        }
        if (WebWizard.ActiveStep.ID.ToString() == "ChildSponsor")
        {
            ddlProgramSponsorship.SelectedIndex = 0;
            txtOneTimeDonation.Text = "";

        }
        if (WebWizard.ActiveStep.ID.ToString() == "ProgrSponsor")
        {
            txtOneTimeDonation.Text = "";
            txtChildrenToSponsor.Text = "";
        }
        if (WebWizard.ActiveStep.ID.ToString() == "OneTime")
        {
            ddlProgramSponsorship.SelectedIndex = 0;
            txtChildrenToSponsor.Text = "";
        }
        if (WebWizard.ActiveStep.ID.ToString() == "Rebuttals")
        {
            
        }
        if (WebWizard.ActiveStep.ID.ToString() == "FAQ")
        {
            
        }
        if (WebWizard.ActiveStep.ID.ToString() == "Disposition")
        {
            if (Sender == 6)
            {
                ddlPaymentType.SelectedIndex = 1;
            }
            if (SubmitData())
            {

                AddIntroData(); //Add the data if the sponsor has special requests.

                switch (Sender)
                {
                    case 3: //Child Sponsor
                        if (!string.IsNullOrEmpty(txtChildrenToSponsor.Text) && System.Text.RegularExpressions.Regex.IsMatch(txtChildrenToSponsor.Text, @"^[0-9]{0,2}?$"))
                        {
                            switch (ddlPaymentFrequency.SelectedValue.ToString())
                            {
                                case "5":
                                App.PutCallData("PaymentAmount", (Convert.ToInt32(txtChildrenToSponsor.Text) * 28).ToString());
                                break;
                                case "4":
                                App.PutCallData("PaymentAmount", ((Convert.ToInt32(txtChildrenToSponsor.Text) * 28)*3).ToString());
                                break;
                                case "1":
                                App.PutCallData("PaymentAmount", ((Convert.ToInt32(txtChildrenToSponsor.Text) * 28)*12).ToString());
                                break;
                                default:
                                App.PutCallData("PaymentAmount", "0");
                                break;
                            }
                            App.PutCallData("ChildrenToSponsor", txtChildrenToSponsor.Text);
                            App.PutCallData("OneTimeAmount", "");
                            WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.Disposition);
                        }
                        else
                        {
                            WebWizard.ActiveStepIndex = Sender;
                            return;
                        }
                        break;
                    case 4: //Program Sponsor
                        if (ddlProgramSponsorship.SelectedIndex > 0)
                        {
                            switch (ddlPaymentFrequency.SelectedValue.ToString())
                            {
                                case "5":
                                    App.PutCallData("PaymentAmount", (Convert.ToInt32(ddlProgramSponsorship.SelectedValue)).ToString());
                                    break;
                                case "4":
                                    App.PutCallData("PaymentAmount", (Convert.ToInt32(ddlProgramSponsorship.SelectedValue) * 3).ToString());
                                    break;
                                case "1":
                                    App.PutCallData("PaymentAmount", (Convert.ToInt32(ddlProgramSponsorship.SelectedValue) * 12).ToString());
                                    break;
                                default:
                                    App.PutCallData("PaymentAmount", "0");
                                    break;
                            }
                            App.PutCallData("OneTimeAmount", "");
                            WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.Disposition);
                        }
                        else
                        {
                            WebWizard.ActiveStepIndex = Sender;
                            return;
                        }
                        break;
                    case 5: //One Time Payment
                        if (!string.IsNullOrEmpty(txtOneTimeDonation.Text) && System.Text.RegularExpressions.Regex.IsMatch(txtOneTimeDonation.Text.Replace(",", "").Replace("$", ""), @"^[0-9]+$"))
                        {
                            lblOneTimeAmtError.Visible = false;
                            App.PutCallData("OneTimeAmount", txtOneTimeDonation.Text.Replace(",", "").Replace("$", "").Replace(".00", ""));
                            App.PutCallData("PaymentAmount", txtOneTimeDonation.Text.Replace(",", "").Replace("$", "").Replace(".00", ""));
                            App.PutCallData("PaymentFrequency", "0");
                            WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.Disposition);
                        }
                        else
                        {
                            lblOneTimeAmtError.Visible = true;
                            WebWizard.ActiveStepIndex = Sender;
                            return;
                        }
                        break;
                    case 6: //More Information
                        App.PutCallData("PaymentAmount", "");
                        App.PutCallData("PaymentFrequency", "");
                        App.PutCallData("OneTimeAmount", "");
                        App.PutCallData("webusercode", "SF");
                        App.PutCallData("Disposition", "SC");
                        App.PutCallData("MotivationCode", "118973");
                        WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.Disposition);
                        break;
                }
                App.CommitCallData();
                Response.Redirect("../DispositionComplete.aspx");                
            }
            else
            {
                WebWizard.ActiveStepIndex = Sender;
                return;
            }
        }

    }
    protected void AddIntroData()
    {
        if (ddlGirlOrBoy.SelectedIndex > 0)
        {
            App.PutCallData("PreferredGender", ddlGirlOrBoy.SelectedValue);
        }
        if (ddlAgePreference.SelectedIndex > 0)
        {
            App.PutCallData("PreferredAge", ddlAgePreference.SelectedValue);
        }
        if (ddlRegionPreference.SelectedIndex > 0)
        {
            App.PutCallData("PreferredRegion", ddlRegionPreference.SelectedValue);
        }
        if (ddlCountryPreference.SelectedIndex > 0)
        {
            App.PutCallData("PreferredCountry", ddlCountryPreference.SelectedValue);
        }
        return;

    }
    protected bool SubmitData()
    {
        lblError.Text = "";

        bool bCustValid = false;
        bool ccCheck = false;
        bool ecCheck = false;
        bool bShipValid = false;
        int yearval = Convert.ToInt32(DateTime.Today.ToString("yy"));
        //int result;

        //put payment type in db
        App.PutCallData("PaymentType", ddlPaymentType.SelectedItem.Value);

        //if There is going to be a payment.
        if (ddlPaymentType.SelectedValue != "NO" && ddlPaymentType.SelectedIndex != 0)
        {
            if (ddlPaymentFrequency.SelectedIndex == 0)
            {
                lblError.Text = "Select a Payment Frequency";
                return false;
            }
            App.PutCallData("PaymentFrequency", ddlPaymentFrequency.SelectedValue);
            App.PutCallData("PaymentType", ddlPaymentType.SelectedItem.Value);
            //Check for ccExp length to be correct
            if (tbExpDate.Text.Length == 4 && ddlPaymentType.SelectedValue == "CC")
            {
                //Make sure cc data fields have data and fall within specified parameters
                ccCheck = !(string.IsNullOrEmpty(ddlCardType.SelectedItem.Text) ||
                    string.IsNullOrEmpty(tbCCNumber.Text) ||
                    string.IsNullOrEmpty(tbAccHolder.Text) ||
                    //string.IsNullOrEmpty(tbCCV.Text) ||
                    string.IsNullOrEmpty(tbExpDate.Text) ||
                    (Convert.ToInt32(tbExpDate.Text.Substring(2, 2)) == yearval && Convert.ToInt32(tbExpDate.Text.Substring(0, 2)) < Convert.ToInt32(DateTime.Today.ToString("MM")))) &&
                    (Convert.ToInt32(tbExpDate.Text.Substring(2, 2)) >= yearval) &&
                    (Convert.ToInt32(tbExpDate.Text.Substring(2, 2)) <= (yearval + 10)) &&
                    (Convert.ToInt32(tbExpDate.Text.Substring(0, 2)) >= 1 && Convert.ToInt32(tbExpDate.Text.Substring(0, 2)) <= 12) &&
                    (System.Text.RegularExpressions.Regex.IsMatch(tbExpDate.Text, @"^[\d]+$"));
            }
            else
            {
                if (ddlPaymentType.SelectedValue != "CC")
                { //Do Nothing
                }
                else
                {
                    lblError.Text = "Enter Valid Expiration Date<br/>";
                    ccCheck = false;
                }
            }

            ecCheck = !(string.IsNullOrEmpty(tbBankName.Text) ||
                string.IsNullOrEmpty(tbAccHolder.Text) ||
                string.IsNullOrEmpty(tbAccNum.Text) ||
                string.IsNullOrEmpty(tbRoutNum.Text)) &&
                (System.Text.RegularExpressions.Regex.IsMatch(tbAccNum.Text, @"^[\d]+$")) &&
                (System.Text.RegularExpressions.Regex.IsMatch(tbRoutNum.Text, @"^[\d]+$")) &&
                App.ValidateRoutingNumber(Convert.ToInt32(tbRoutNum.Text));

            if (ddlPaymentType.SelectedIndex == 2) // debit/credit
            {
                if (ccCheck)
                {
                    App.PutCallData("NameOnAccount", App.EnryptData(tbAccHolder.Text.Replace(",", "")));
                    App.PutCallData("CCType", App.EnryptData(ddlCardType.SelectedItem.Value));
                    App.PutCallData("CCNumber", App.EnryptData(tbCCNumber.Text));
                    App.PutCallData("CCExpDate", App.EnryptData((tbExpDate.Text.Substring(0, 2) + "/" + tbExpDate.Text.Substring(2, 2))));
                    if (!string.IsNullOrEmpty(txtChildrenToSponsor.Text))
                    {
                        App.PutCallData("webusercode", "S1");
                        App.PutCallData("Disposition", "SC");
                        App.PutCallData("ProductType", "ICS");
                        App.PutCallData("MotivationCode", "118970");
                    }
                    else
                    {
                        if (ddlProgramSponsorship.SelectedIndex > 0)
                        {
                            App.PutCallData("webusercode", "S4");
                            App.PutCallData("Disposition", "SC");
                            App.PutCallData("ProductType", "CM");
                            App.PutCallData("MotivationCode", "118970");
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(txtOneTimeDonation.Text))
                            {
                                App.PutCallData("webusercode", "S7");
                                App.PutCallData("Disposition", "SC");
                                App.PutCallData("ProductType", "1x");
                                App.PutCallData("MotivationCode", "118971");
                            }
                            else
                            {
                                App.PutCallData("webusercode", "ER");
                                App.PutCallData("Disposition", "ER");
                            }
                        }
                    }
                }
                else
                {
                    lblError.Text += "Invalid Card Info<br/>";
                    return false;
                }
            }
            else if (ddlPaymentType.SelectedIndex == 3) // echeck
            {
                App.PutCallData("PaymentType", ddlPaymentType.SelectedItem.Value);

                if (ecCheck)
                {

                    App.PutCallData("NameOnAccount", App.EnryptData(tbAccHolder.Text.Replace(",", "")));
                    App.PutCallData("BankCity", App.EnryptData(tbBankCity.Text.Replace(",", "")));
                    App.PutCallData("BankState", App.EnryptData(tbBankState.Text.Replace(",", "")));
                    App.PutCallData("AccountNumber", App.EnryptData(tbAccNum.Text));
                    App.PutCallData("RoutingNumber", App.EnryptData(tbRoutNum.Text));
                    App.PutCallData("AccountType", ddlAccType.SelectedItem.Text);
                    App.PutCallData("BankName", tbBankName.Text.Replace(",", ""));
                    if (!string.IsNullOrEmpty(txtChildrenToSponsor.Text))
                    {
                        App.PutCallData("webusercode", "S2");
                        App.PutCallData("Disposition", "SC");
                        App.PutCallData("ProductType", "ICS");
                        App.PutCallData("MotivationCode", "118970");
                    }
                    else
                    {
                        if (ddlProgramSponsorship.SelectedIndex > 0)
                        {
                            App.PutCallData("webusercode", "S5");
                            App.PutCallData("Disposition", "SC");
                            App.PutCallData("ProductType", "CM");
                            App.PutCallData("MotivationCode", "118970");
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(txtOneTimeDonation.Text))
                            {
                                App.PutCallData("webusercode", "S8");
                                App.PutCallData("Disposition", "SC");
                                App.PutCallData("ProductType", "1x");
                                App.PutCallData("MotivationCode", "118971");
                            }
                            else
                            {
                                App.PutCallData("webusercode", "ER");
                                App.PutCallData("Disposition", "ER");
                            }
                        }
                    }
                }
                else
                {
                    lblError.Text += "Invalid Bank Info<br/>";
                    return false;
                }
            }
            else if (ddlPaymentType.SelectedIndex == 4) //Mail letter in
            {
                if (!string.IsNullOrEmpty(txtChildrenToSponsor.Text))
                {
                    App.PutCallData("webusercode", "S3");
                    App.PutCallData("Disposition", "SC");
                    App.PutCallData("ProductType", "ICS");
                    App.PutCallData("MotivationCode", "118972");
                }
                else
                {
                    if (ddlProgramSponsorship.SelectedIndex > 0)
                    {
                        App.PutCallData("webusercode", "S6");
                        App.PutCallData("Disposition", "SC");
                        App.PutCallData("ProductType", "CM");
                        App.PutCallData("MotivationCode", "118972");
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(txtOneTimeDonation.Text))
                        {
                            App.PutCallData("webusercode", "S9");
                            App.PutCallData("Disposition", "SC");
                            App.PutCallData("ProductType", "1x");
                            App.PutCallData("MotivationCode", "118971");
                        }
                        else
                        {
                            App.PutCallData("webusercode", "ER");
                            App.PutCallData("Disposition", "ER");
                        }
                    }
                }
            }
            else
            {
                lblError.Text += "Please select a payment method." + Environment.NewLine;
                return false;
            }
        }


        bCustValid = !(string.IsNullOrEmpty(tbFirstName.Text) ||
        string.IsNullOrEmpty(tbLastName.Text) ||
        string.IsNullOrEmpty(tbAddr.Text) ||
        string.IsNullOrEmpty(tbZip.Text) ||
        string.IsNullOrEmpty(ddlCity.SelectedItem.Text) ||
        string.IsNullOrEmpty(ddlState.SelectedItem.Text) ||
        string.IsNullOrEmpty(ddlCountry.SelectedItem.Text)) &&
        (ddlCity.SelectedIndex > 0) &&
        (ddlTitle.SelectedIndex > 0) &&
        (ddlGender.SelectedIndex > 0);

        bShipValid = !(string.IsNullOrEmpty(tbShipAddr.Text) ||
        string.IsNullOrEmpty(tbShipZip.Text) ||
        string.IsNullOrEmpty(ddlShipCity.SelectedItem.Text) ||
        string.IsNullOrEmpty(ddlShipState.SelectedItem.Text) ||
        string.IsNullOrEmpty(ddlShipCountry.SelectedItem.Text)) &&
        (ddlShipCity.SelectedIndex > 0) &&
        (tbShipZip.Text.Length >= 5) &&
        (tbShipZip.Text.Length <= 7);



        if (bCustValid)
        {
            App.PutCallData("Title", ddlTitle.Text.Replace(",", ""));
            App.PutCallData("FirstName", tbFirstName.Text.Replace(",", ""));
            App.PutCallData("LastName", tbLastName.Text.Replace(",", ""));
            App.PutCallData("MiddleInitial", tbMiddleInitial.Text.Replace(",", ""));
            App.PutCallData("GenderOfSponsor", ddlGender.SelectedValue);
            App.PutCallData("Address", tbAddr.Text.Replace(",", ""));
            if (!string.IsNullOrEmpty(tbAddr2.Text))
                App.PutCallData("Address2", tbAddr2.Text.Replace(",", ""));
            else
                App.PutCallData("Address2", "");
            App.PutCallData("Zip", tbZip.Text.Replace(",", ""));
            App.PutCallData("City", ddlCity.SelectedItem.Text.Replace(",", ""));
            App.PutCallData("State", ddlState.SelectedItem.Text);
            App.PutCallData("Country", ddlCountry.SelectedItem.Text);
            if (chkWantsCD.Checked)
                App.PutCallData("WantsCD", "N");
            else
                App.PutCallData("WantsCD", "Y");


            //this is what happens if the shipping info is not visible
            if (!cbShipping.Checked)
            {
                App.PutCallData("ShippingAddress", tbAddr.Text.Replace(",", ""));
                if (!string.IsNullOrEmpty(tbAddr2.Text))
                    App.PutCallData("ShippingAddress2", tbAddr2.Text.Replace(",", ""));
                else
                    App.PutCallData("ShippingAddress2", "");
                App.PutCallData("ShippingZip", tbZip.Text.Replace(",", ""));
                App.PutCallData("ShippingCity", ddlCity.SelectedItem.Text.Replace(",", ""));
                App.PutCallData("ShippingState", ddlState.SelectedItem.Text);
                App.PutCallData("ShippingCountry", ddlCountry.SelectedItem.Text);
            }
            else
            {
                if (bShipValid)
                {
                    App.PutCallData("ShippingAddress", tbShipAddr.Text.Replace(",", ""));
                    if (!string.IsNullOrEmpty(tbShipAddr.Text))
                        App.PutCallData("ShippingAddress2", tbShipaddr2.Text.Replace(",", ""));
                    else
                        App.PutCallData("ShippingAddress2", "");
                    App.PutCallData("ShippingZip", tbShipZip.Text.Replace(",", ""));
                    App.PutCallData("ShippingCity", ddlShipCity.SelectedItem.Text.Replace(",", ""));
                    App.PutCallData("ShippingState", ddlShipState.SelectedItem.Text);
                    App.PutCallData("ShippingCountry", ddlShipCountry.SelectedItem.Text);
                }
                else
                {
                    lblError.Text += "Enter Valid Shipping Info<br/>";
                    return false;
                }
            }
        }
        else
        {
            if (ddlCity.Items.Count > 1 && ddlCity.SelectedIndex == 0)
            {
                lblError.Text += "Select a City<br/>";
                return false;
            }
            else
            {
                lblError.Text += "Enter Valid Customer Data<br/>";
                return false;
            }
            
        }

        if ((!string.IsNullOrEmpty(tbPhone.Text)) && ((tbPhone.Text.Length != 10) || !(System.Text.RegularExpressions.Regex.Match(tbPhone.Text, @"^[1-9]\d{2}[1-9]\d{6}$").Success)))
        {
            lblError.Text += "Invalid Phone<br/>";
            return false;
        }
        else
        {
            if (!string.IsNullOrEmpty(tbPhone.Text))
                App.PutCallData("Phone", tbPhone.Text);
        }

        if ((!string.IsNullOrEmpty(tbCellPhone.Text)) && ((tbCellPhone.Text.Length != 10) || !(System.Text.RegularExpressions.Regex.Match(tbCellPhone.Text, @"^[1-9]\d{2}[1-9]\d{6}$").Success)))
        {
            lblError.Text += " Invalid Cell Phone<br/>";
            return false;
        }
        else
        {
            if (!string.IsNullOrEmpty(tbCellPhone.Text))
                App.PutCallData("CellPhone", tbCellPhone.Text);
        }

        if ((!string.IsNullOrEmpty(tbEmail.Text)) && !App.IsValidEmail(tbEmail.Text))
        {
            lblError.Text += "Invalid Email<br/>";
            return false;
        }
        else
        {
            if (!string.IsNullOrEmpty(tbEmail.Text))
                App.PutCallData("Email", tbEmail.Text);
        }

        return true;
    }

    ///***************************
    protected void ddlPaymentType_SelectedIndexChanged(object sender, EventArgs e)
    {
        //if (string.IsNullOrEmpty(txtOneTimeDonationAmt.Text) && string.IsNullOrEmpty(txtNumberOfChildren.Text))
        //{
        //    lblError.Text = " You must enter a value for Number of Children OR One Time Donation Amount ";
        //    lblError.Visible = true;
        //    ddlPaymentType.SelectedIndex = 0;
        //}
        //else
        //{
            //App.PutCallData("PaymentType", ddlPaymentType.SelectedItem.Value); <<< UNCOMMENT THIS LINE
        switch(ddlPaymentType.SelectedValue)
        {
            case "CC":
                trCCNumber.Visible = true;
                trCCType.Visible = true;
                trCCExpDate.Visible = true;
                trPmtFreq.Visible = true;
                trECAccNum.Visible = false;
                trECBankName.Visible = false;
                trECRoutNum.Visible = false;
                trECAccType.Visible = false;
                trAccHolder.Visible = true;
                trECBankCity.Visible = false;
                trECBankState.Visible = false;
                tbAccHolder.Text = "";
                tbAccNum.Text = "";
                tbBankName.Text = "";
                tbRoutNum.Text = "";
                tbBankCity.Text = "";
                tbBankState.Text = "";
                break;
            case "EC":    
                trCCNumber.Visible = false;
                trCCType.Visible = false;
                trCCExpDate.Visible = false;
                trPmtFreq.Visible = true;
                trECAccNum.Visible = true;
                trECBankName.Visible = true;
                trECRoutNum.Visible = true;
                trECAccType.Visible = true;
                trAccHolder.Visible = true;
                trECBankCity.Visible = true;
                trECBankState.Visible = true;
                tbCCNumber.Text = "";
                tbExpDate.Text = "";
                //trCCNumber.Visible = false;
                //trCCType.Visible = false;
                //trCCExpDate.Visible = false;
                ////trCCV.Visible = false;
                //trECAccNum.Visible = true;
                //trECBankName.Visible = true;
                //trECCheckNum.Visible = true;
                //trECRoutNum.Visible = true;
                //trECAccType.Visible = true;
                //trECAccHolder.Visible = true;
                //tbCCNumber.Text = "";
                //tbExpDate.Text = "";
                break;

            case "MA":
                trPmtFreq.Visible = true;
                trCCNumber.Visible = false;
                trCCType.Visible = false;
                trCCExpDate.Visible = false;
                trECAccNum.Visible = false;
                trECBankName.Visible = false;
                trECRoutNum.Visible = false;
                trECAccType.Visible = false;
                trAccHolder.Visible = false;
                trECBankCity.Visible = false;
                trECBankState.Visible = false;

                //trCCNumber.Visible = false;
                //trCCType.Visible = false;
                //trCCExpDate.Visible = false;
                //trECAccNum.Visible = false;
                //trECBankName.Visible = false;
                //trECCheckNum.Visible = false;
                //trECRoutNum.Visible = false;
                //trECAccType.Visible = false;
                tbCCNumber.Text = "";
                tbExpDate.Text = "";
                tbAccHolder.Text = "";
                tbAccNum.Text = "";
                tbBankName.Text = "";
                tbRoutNum.Text = "";
                tbBankCity.Text = "";
                tbBankState.Text = "";
                break;
            default:
                trCCNumber.Visible = false;
                trCCType.Visible = false;
                trCCExpDate.Visible = false;
                trPmtFreq.Visible = false;
                trECAccNum.Visible = false;
                trECBankName.Visible = false;
                trECRoutNum.Visible = false;
                trECAccType.Visible = false;
                trAccHolder.Visible = false;
                trECBankCity.Visible = false;
                trECBankState.Visible = false;

                //trCCNumber.Visible = false;
                //trCCType.Visible = false;
                //trCCExpDate.Visible = false;
                //trECAccNum.Visible = false;
                //trECBankName.Visible = false;
                //trECCheckNum.Visible = false;
                //trECRoutNum.Visible = false;
                //trECAccType.Visible = false;
                tbCCNumber.Text = "";
                tbExpDate.Text = "";
                tbAccHolder.Text = "";
                tbAccNum.Text = "";
                tbBankName.Text = "";
                tbRoutNum.Text = "";
                tbBankCity.Text = "";
                tbBankState.Text = "";
                break;
            
    }
        

        //    }
        //}
        //if (ddlPaymentType.SelectedValue == "No")
        //{
        //    WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.MailLetter);
        //}

}

    protected void cbShipping_CheckedChanged(object sender, EventArgs e)
    {
        if (cbShipping.Checked)
        {

            trShipAddr.Visible = true;
            trShipAddr2.Visible = true;
            trShipCity.Visible = true;
            trShipState.Visible = true;
            trShipCountry.Visible = true;
            trShipZip.Visible = true;
            trShipLbl.Visible = true;
            trShipSearchZip.Visible = true;

        }
        else
        {
            trShipAddr.Visible = false;
            trShipAddr2.Visible = false;
            trShipCity.Visible = false;
            trShipState.Visible = false;
            trShipCountry.Visible = false;
            trShipZip.Visible = false;
            trShipLbl.Visible = false;
            trShipSearchZip.Visible = false;
        }
    }
    protected void ddlRegionPreference_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblCountrySelected.Visible = true;
        lblCountrySelected.Text = "information about " + ddlRegionPreference.SelectedItem.Text + " and the needs of the children and families living there. We’ll also include";
        ddlCountryPreference.Items.Clear();
        ListItem[] items;
        items = new ListItem[5];

        for (int i = 0; i <= 4; i++)
        {
            items[i] = new ListItem();
            if (i == 0)
            {
                items[0].Selected = true;
                items[0].Text = "If Donor Wants to Specify Country";
                items[0].Value = "";
                ddlCountryPreference.Items.Insert(0, items[0]);
            }
            else
            {
                items[i].Selected = false;
                items[i].Text = "";
                items[i].Value = "";
            }
        }
        switch (ddlRegionPreference.SelectedValue)
        {
            case "Asia":
                items[1].Text = "Bangladesh";
                items[1].Value = "Bangladesh";
                items[2].Text = "Nepal";
                items[2].Value = "Nepal";
                items[3].Text = "Philippines";
                items[3].Value = "Philippines";
                break;
            case "Africa":
                items[1].Text = "Ethiopia";
                items[1].Value = "Ethiopia";
                items[2].Text = "Malawi";
                items[2].Value = "Malawi";
                items[3].Text = "Mali";
                items[3].Value = "Mali";
                items[4].Text = "Mozambique";
                items[4].Value = "Mozambique";
                break;
            case "Latin America":
                items[1].Text = "Bolivia";
                items[1].Value = "Bolivia";
                items[2].Text = "El Salvador";
                items[2].Value = "El Salvador";
                items[3].Text = "Haiti";
                items[3].Value = "Haiti";
                break;
            case "Middle East":
                items[1].Text = "Egypt";
                items[1].Value = "Egypt";
                break;
            case "USA":
                items[1].Text = "Southern Area";
                items[1].Value = "Southern Area";
                items[2].Text = "Western Area";
                items[2].Value = "Western Area";
                break;
        }
        for (int i = 1; i <= 4; i++)
        {
            if (items[i].Value.ToString() != "")
            {
                ddlCountryPreference.Items.Add(items[i]);
            }
        }
    }
    protected void btnSearchZip_Click(object sender, EventArgs e)
    {
        ValidateZip(tbZip.Text.ToUpper(), ddlCity, ddlState, ddlCountry);
        if (cbShipping.Checked)
        {
            if (ddlShipCountry.SelectedItem.Text == "CANADA")
            {
                ddlPaymentType.Items[2].Enabled = false;
            }
        }
        else if (ddlCountry.SelectedItem.Text == "CANADA")
        {
            ddlPaymentType.Items[2].Enabled = false;
        }
        else
        {
            ddlPaymentType.Items[2].Enabled = true;
        }
    }

    protected void btnShipSearchZip_Click(object sender, EventArgs e)
    {
        ValidateZip(tbShipZip.Text.ToUpper(), ddlShipCity, ddlShipState, ddlShipCountry);
        if (cbShipping.Checked)
        {
            if (ddlShipCountry.SelectedItem.Text == "CANADA")
            {
                ddlPaymentType.Items[2].Enabled = false;
            }
            else
            {
                ddlPaymentType.Items[2].Enabled = true;
            }
        }
    }

    protected void ValidateZip(string strZip, DropDownList ddl_City, DropDownList ddl_State, DropDownList ddl_Country)
    {

        ddl_City.Items.Clear();
        ddl_State.Items.Clear();
        ddl_City.Enabled = true;
        ddl_State.Enabled = true;
        if (System.Text.RegularExpressions.Regex.IsMatch(strZip.ToUpper(), @"(^\d{5}$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} +\d{1}[A-Z]{1}\d{1}$)"))
        {
            try
            {
                com.codebump.GeoPlaces g = new com.codebump.GeoPlaces();

                com.codebump.AuthenticationHeader authHeader = new com.codebump.AuthenticationHeader();
                authHeader.SessionID = "6JV2ZA4lgG1YRR+HG+JU27QWk9zI3rpRWY2iG2mzNPcdieEQXfQv0OUY0EZ+pOTxhwI1NCuF7b2oY+U/bozy0mhjFHmpaXvb";
                g.AuthenticationHeaderValue = authHeader;

                com.codebump.GeoPlaceDistance[] places = g.GetPlacesInside(strZip, "");

                for (int i = 0; i < places.Length; i++)
                {
                    com.codebump.GeoPlaceDistance place = places[i];
                    ListItem li = new ListItem(place.ToPlace, place.ToState + "," + place.ToPlace);
                    ddl_City.Items.Add(li);
                }

                ddl_State.Items.Add(ddl_City.SelectedItem.Value.Split(",".ToCharArray())[0]);

                //ddlCity.SelectedIndex = 0;

                if (System.Text.RegularExpressions.Regex.IsMatch(strZip.ToUpper(), @"(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)"))
                {
                    ddl_Country.SelectedValue = "CANADA";
                }
                else
                {
                    ddl_Country.SelectedValue = "USA";
                }

                if (ddl_City.Items.Count <= 0)
                {
                    ddl_City.Items.Add("No Cities Found");
                    ddl_State.Items.Add("No States Found");
                    ddl_City.Enabled = false;
                    ddl_State.Enabled = false;
                }
                else
                {
                    ddl_City.Items.Insert(0, "Select a City");
                    ddl_City.SelectedIndex = 0;
                    ddl_State.Enabled = false;
                }
            }
            catch
            {
                ddl_City.Items.Add("No Cities Found");
                ddl_State.Items.Add("No States Found");
                ddl_City.Enabled = false;
                ddl_State.Enabled = false;
            }
        }
        else
        {
            ddl_City.Items.Add("Invalid Zip");
            ddl_City.Enabled = false;
            ddl_State.Items.Add("Invalid Zip");
            ddl_State.Enabled = false;
        }
    }

    protected void ddlCountryPreference_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlCountryPreference.SelectedIndex != 0)
        {
            lblCountrySelected.Visible = true;
            lblCountrySelected.Text = "information about " + ddlCountryPreference.SelectedItem.Text + " and the needs of the children and families living there. We’ll also include";
        }

    }
    protected void txtOneTimeDonation_TextChanged(object sender, EventArgs e)
    {
        lblOneTimeDonationAmt.Text = txtOneTimeDonation.Text;
    }
}